<?php
function lite_theme_setup() {
	//添加缩略图支持
	add_theme_support('post-thumbnails');

	//HTML5表单
	add_theme_support('html5', array(
		'search-form',
		'comment-form',
		'comment-list',
		'gallery',
		'caption',
		'script',
		'style',
	));

	// 自定义导航菜单
	register_nav_menus(
		array(
			'primary_nav' => '主导航',
		)
	);
}
add_action('after_setup_theme', 'lite_theme_setup');

//添加HTML编辑器自定义快捷标签按钮
function lite_add_button_mce() {
	echo '<script type="text/javascript">
			QTags.addButton("pre", "pre", "<pre>", "</pre>");
		</script>';
}
add_action('after_wp_tiny_mce', 'lite_add_button_mce');

//加载CSS
function lite_load_scripts() {
	wp_enqueue_style('style', get_theme_file_uri('style.css'));
}
add_action('wp_enqueue_scripts', 'lite_load_scripts');

//文章阅读数
function lite_set_post_views() {
	global $post;

	if(!isset($post->ID) || !is_singular()) return;
	$cookie = 'views_' . $post->ID;
	if(isset($_COOKIE[$cookie])) return;

	$views = get_post_meta($post->ID, 'views', true);
	$views = $views ? $views+1 : 1;

	update_post_meta($post->ID, 'views', $views);
	setcookie($cookie, $views, (current_time('timestamp') + DAY_IN_SECONDS));
}
add_action('get_header', 'lite_set_post_views');

function lite_post_views() {
	global $post;
	$views = get_post_meta($post->ID, 'views', true);
	echo $views ? $views : 0;
}

//分类、标签SEO标题
function lite_add_cat_metabox() {
	echo '<div class="form-field">
			<label for="seo-title">SEO标题</label>
			<input type="text" size="40" value="" id="seo-title" name="seo_title">
			<p>SEO标题只显示在Title标签中，不会在其他地方显示</p>
		</div>';
}
add_action('category_add_form_fields', 'lite_add_cat_metabox');
add_action('post_tag_add_form_fields', 'lite_add_cat_metabox');

function lite_edit_cat_metabox($tag) {
	$value = get_term_meta($tag->term_id, 'seo_title', true);
	echo '<tr class="form-field">
			<th scope="row"><label for="seo-title">SEO标题</label></th>
			<td>
				<input type="text" size="40" value="' . $value . '" id="seo-title" name="seo_title" />
				<p class="description">SEO标题只显示在Title标签中，不会在其他地方显示</p>
			</td>
		</tr>';
}
add_action('category_edit_form_fields', 'lite_edit_cat_metabox', 10, 1);
add_action('post_tag_edit_form_fields', 'lite_edit_cat_metabox', 10, 1);

function lite_update_metadata($term_id) {
	if(!isset($_POST['seo_title']) || !current_user_can('manage_categories')) return $term_id;
	$data = sanitize_text_field($_POST['seo_title']);
	update_term_meta($term_id, 'seo_title', $data);
}

function lite_delete_metadata($term_id) {
	if(!current_user_can('manage_categories')) return $term_id;
	delete_term_meta($term_id, 'seo_title');
}
add_action('created_category', 'lite_update_metadata', 10, 1);
add_action('edited_category', 'lite_update_metadata', 10, 1);
add_action('delete_category', 'lite_delete_metadata', 10, 1);

add_action('created_post_tag', 'lite_update_metadata', 10, 1);
add_action('edited_post_tag', 'lite_update_metadata', 10, 1);
add_action('delete_post_tag', 'lite_delete_metadata', 10, 1);

//Title标题
function lite_filter_title($title) {
	if(is_category() || is_tag()) {
		global $cat, $tag_id;
		if(is_tag()) $cat = $tag_id;
		$seo_title = get_term_meta($cat, 'seo_title', true);
		if($seo_title) {
			$title['title'] = $seo_title;
		}
	}

	if(is_author()) $title['title'] = '作者：' . $title['title'];

	return $title;
}
add_filter('document_title_parts', 'lite_filter_title', 10, 1);

//Description标签
function lite_seo_description() {
	global $post;

	$description = '';
	if(is_home()) $description = get_option('home_description');
	if((is_category() || is_tag()) && category_description()) {
		$description = wp_strip_all_tags(category_description(), true);
	}
	if(is_single() || is_page()) {
		if($post->post_excerpt) {
			$description = $post->post_excerpt;
		} else {
			$description = mb_substr(esc_attr(wp_strip_all_tags($post->post_content, true)), 0, 200, 'utf-8');
		}
	}

	echo $description;
}

//菜单过滤
function lite_nav_class($classes) {
    foreach($classes as $key=>$val) {
		if('current-menu-item' == $val) {
			$classes[$key] = 'current';
		} else {
			unset($classes[$key]);
		}
    }
    return $classes;
}
add_filter('nav_menu_css_class', 'lite_nav_class', 10, 1);

//添加常规选项
function lite_add_setting() {
	register_setting('general', 'home_description', [
		'type' => 'string',
		'sanitize_callback' => 'sanitize_text_field',
		'default' => NULL,
	]);

	add_settings_field('home_description', '<label for="home-description">首页描述</label>', function() {
		echo '<textarea name="home_description" rows="3" cols="50" id="home-description" class="regular-text code">' . get_option('home_description') . '</textarea>';
	}, 'general');

	register_setting('general', 'analytics_code', [
		'type' => 'string',
		'sanitize_callback' => 'esc_textarea',
		'default' => NULL,
	]);

	add_settings_field('analytics_code', '<label for="analytics-code">统计代码</label>', function() {
		echo '<textarea name="analytics_code" rows="3" cols="50" id="analytics-code" class="regular-text code">' . get_option('analytics_code') . '</textarea>';
	}, 'general');
}
add_action('admin_init', 'lite_add_setting');

//小工具
function lite_register_widget() {
	register_sidebar(array(
		'name' => 'Loop循环中',
		'id' => 'inloop',
		'description' => '',
		'class' => '',
		'before_widget' => '<div class="ad inloop">',
		'after_widget' => '</div>',
		'before_title' => '<h3>',
		'after_title' => '</h3>',
	));
	register_sidebar(array(
		'name' => '文章内容前',
		'id' => 'content-before',
		'description' => '',
		'class' => '',
		'before_widget' => '<div class="ad content-before">',
		'after_widget' => '</div>',
		'before_title' => '<h3>',
		'after_title' => '</h3>',
	));
	register_sidebar(array(
		'name' => '文章内容后',
		'id' => 'content-after',
		'description' => '',
		'class' => '',
		'before_widget' => '<div class="ad content-after">',
		'after_widget' => '</div>',
		'before_title' => '<h3>',
		'after_title' => '</h3>',
	));
}
add_action('widgets_init', 'lite_register_widget');

?>
